#include "stdafx.h"
#include "QGHThreadTaskItem.h"

class QGHThreadTaskItemPrivate{
public:
	
	QEventLoop*				m_pEventLoop;
	QWaitCondition			m_waitCond;
	QMutex					m_mutex;
};

QGHThreadTaskItem::QGHThreadTaskItem(QObject *parent)
	: QObject(parent)
{
	m_nTaskType = eEmpty;

	m_pPrivate = new QGHThreadTaskItemPrivate;
	m_pPrivate->m_pEventLoop = new QEventLoop(this);
	connect(this,SIGNAL(finishedTask()),m_pPrivate->m_pEventLoop,SLOT(quit()));
}

QGHThreadTaskItem::~QGHThreadTaskItem()
{
	delete m_pPrivate;
}

void QGHThreadTaskItem::finishedTaskItem()
{
	emit finishedTask();
}

void QGHThreadTaskItem::wait()
{
	m_pPrivate->m_pEventLoop->exec();
}

void QGHThreadTaskItem::synchLock()
{
	m_pPrivate->m_mutex.lock();
	m_pPrivate->m_waitCond.wait(&m_pPrivate->m_mutex);
	m_pPrivate->m_mutex.unlock();
}

void QGHThreadTaskItem::synchUnLock()
{
	m_pPrivate->m_mutex.lock();
	m_pPrivate->m_waitCond.wakeOne();
	m_pPrivate->m_mutex.unlock();
}
